﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jzy.BLL.Mail;
using Jzy.Model.Mail;

using Jzy.Model;
using System.Collections.Generic;
using Jzy.Core.Common;
using Jzy.Core.Common.Web;
using Frame.Lib.Common;
using Frame.Lib.Web;
using Frame.Lib.Utilities;
using Frame.Lib.Core.Module;
using Frame.Lib.Enums;

public partial class WebMail_List :PageList
{
    private WebMailBO _mail;
    private MailFolderBO _mailFolder;
    WebMailAccountBO _account ;

    public int FolderID
    {
        get
        {
            return int.Parse(Request["FolderID"]);
        }

    }


    private PagerSortSetting setting;

    #region ViewState值
    private MailStatus Status
    {
        get
        {
            return (MailStatus)ViewState["Status"];
        }
        set
        {
            ViewState["Status"] = value;
        }
    }
    #endregion
   
    protected void Page_Load(object sender, EventArgs e)
    {
        _mail = new WebMailBO(AppSession.UserID);
        _mailFolder = new MailFolderBO(AppSession.UserID, MailFolderType.外部邮件);
        _account = new WebMailAccountBO(AppSession.UserID);

        setting = this.Pager1.InitPager(new BindViewHandler(BindView));
        CustSort sorter = new CustSort(setting, new BindViewHandler(BindView));
        sorter.Sort(btnName, btnCreator, btnReciever, btnInputTime);

        if (FolderID == (int)MailSysFolder.草稿箱 || FolderID == (int)MailSysFolder.已发送)
        {
            thFrom.Visible = false;
            thTo.Visible = true;
        }
        else
        {
            thFrom.Visible = true;
            thTo.Visible = false;
        }

        if (!IsPostBack)
        {
            Status = MailStatus.All;
            
            _account.BindDrp(drpAccount);

            
            ReceiveMail();                
            
            BindView();
            BindMenu();
        }
        AddAttr();
        strSelectIDs = hidSelectIDs.Value;
        hidSelectIDs.Value = "";

        //未读邮件
        int unreadMailCount = _mail.GetUnreadMailCount();
        ModuleBO module = new ModuleBO();

        object receiveBoxModuleID = module.GetIDByCode(ModuleCode.WebReceiveBox);
        if (receiveBoxModuleID != null)
        {
            ModuleInfo info = module.GetInfo(receiveBoxModuleID);
            //SetModuleName(id,name)
            string strScript = "SetModuleName('" + receiveBoxModuleID + "','" + info.Name + "');";
            if (unreadMailCount > 0)
            {
                strScript = "SetModuleName('" + receiveBoxModuleID + "','<span style=\\'font-weight:bold\\'>" + info.Name + "(" + unreadMailCount + ")</span>');";
            }
            JSUtil.ExcuteJS(strScript, "UnreadMail");
            if (!IsPostBack)
            {
                _mailFolder.AddCustMailFolder(info.UpperID, false);
            }

        }

        JSUtil.SetTableHeight("list", 150); 
    }

    private void ReceiveMail()
    {
        if (FolderID == (int)MailSysFolder.收件箱)//进入收件箱进收外部邮件
        {            
            if (drpAccount.Items.Count == 0)
            {
                ModuleBO module = new ModuleBO();
                object moduleID = module.GetIDByCode(ModuleCode.WebMailAccount);
                int tmpID = 0;
                if (moduleID != null)
                {
                    tmpID = (int)moduleID;
                }
                JSUtil.SelectModule(tmpID, "请输入你的邮件账号，以便于接收外部邮件");
            }
            //else
            //{
            //    WebMailAccountInfo info = _account.GetInfo(drpAccount.SelectedValue);
            //    bool flag = _mail.Receive(info);
            //    if (!flag)
            //    {
            //        JSUtil.Alert("邮件接收失败，请检查邮件的默认账户");
            //    }
            //}
        }
    }

    private void BindMenu()
    {
        List<MailFolderInfo> list = _mailFolder.GetAllBoxList();
        rptMenu.DataSource = list;
        rptMenu.DataBind();
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (strSelectIDs == "")
        {
            return;
        }
        List<int> lstSelectIDs = StrUtil.SplitToInt(strSelectIDs);
        if (FolderID == (int)MailSysFolder.已删除)
        {
            _mail.Delete(lstSelectIDs);
        }
        else
        {
            _mail.Move(lstSelectIDs, (int)MailSysFolder.已删除);
        }
        BindView();
    }

    private void BindView()
    {
        lv.DataKeyNames = new string[] { "ID","Status" };
        LVUtil.Init(lv, lvHeader);
        WebMailAccountInfo info = _account.GetInfo(drpAccount.SelectedValue);

        DataTable dt = _mail.GetList(FolderID, Status,info);

        if (Status == MailStatus.All)
        {
            pnlStatus.Visible = false;
            Dictionary<int, string> allboxs = _mailFolder.GetAllBoxDictionay();
            lblBox.Text = allboxs[FolderID];


            int tmp = 0;
            foreach (DataRow dr in dt.Rows)
            {
                int byt = (int)dr["Status"];
                if (byt == 0)
                {
                    tmp++;
                }
            }

            lblMessage.Text = "共&nbsp;" + dt.Rows.Count + "&nbsp;封&nbsp;,&nbsp;" + tmp + "&nbsp;封未读";
        }
        else
        {
            pnlStatus.Visible = true;
            switch (Status)
            {
                case MailStatus.Forword:
                    lblStatus.Text = "已转发";
                    break;
                case MailStatus.HaveRead:
                    lblStatus.Text = "已读";
                    break;
                case MailStatus.Reply:
                    lblStatus.Text = "已回复";
                    break;
                case MailStatus.UnRead:
                    lblStatus.Text = "未读";
                    break;
            }
            lblBox.Text = "邮件搜索";
            lblMessage.Text = "共&nbsp;" + dt.Rows.Count + "封";
        }
        lv.DataSource = setting.GetDataView(dt);
        lv.DataBind();
    }

    private void AddAttr()
    {
        lnkAdd.HRef = "Edit.aspx?Mode=" + (int)WriteMailMode.New;
        JSUtil.DelConfirm(btnDelete, "邮件");

        JSUtil.ClickBtn(lnkDelete, btnDelete);
        JSUtil.ClickBtn(lnkRefresh, btnRefresh);
    }
    
    protected void rptMenu_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (strSelectIDs != "")
        {
            List<int> lstSelectIDs = StrUtil.SplitToInt(strSelectIDs);
            _mail.Move(lstSelectIDs, int.Parse(e.CommandArgument.ToString()));
            BindView();
        }
    }
    protected void btnForword_Click(object sender, EventArgs e)
    {
        Status = MailStatus.Forword;
        BindView();
    }
    protected void btnAll_Click(object sender, EventArgs e)
    {
        Status = MailStatus.All;
        BindView();
    }
    protected void btnUnRead_Click(object sender, EventArgs e)
    {
        Status = MailStatus.UnRead;
        BindView();
    }
    protected void btnRead_Click(object sender, EventArgs e)
    {
        Status = MailStatus.HaveRead;
        BindView();
    }
    protected void btnReply_Click(object sender, EventArgs e)
    {
        Status = MailStatus.Reply;
        BindView();
    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        BindView();
    }


    protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            Control tdFrom = (Control)e.Item.FindControl("tdFrom");
            Control tdTo = (Control)e.Item.FindControl("tdTo");

            if (FolderID == (int)MailSysFolder.草稿箱 || FolderID == (int)MailSysFolder.已发送)
            {
                tdFrom.Visible = false;
                tdTo.Visible = true;
            }
            else
            {
                tdFrom.Visible = true;
                tdTo.Visible = false;
            }
            int status = (int)lv.DataKeys[((ListViewDataItem)e.Item).DataItemIndex]["Status"];
            if (status == (int)MailStatus.UnRead)
            {
                HtmlTableRow row = (HtmlTableRow)e.Item.FindControl("row");
                row.Style.Add("font-weight", "bold");
            }
        }
    }
    protected void drpAccount_SelectedIndexChanged(object sender, EventArgs e)
    {
        ReceiveMail();
        BindView();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        WebMailAccountInfo info = _account.GetInfo(drpAccount.SelectedValue);
        WebMailBO.Receive(info);
        BindView();
    }
}
